from django.db import models


# 定义出版社模型
class PublisherModel(models.Model):
    # 如果没有主键字段会默认会生成一个ID字段
    publisher_name = models.CharField(max_length=64, null=False, blank=False, help_text="出版社名称", verbose_name="出版社名称")
    contact_person = models.CharField(max_length=64, help_text="联系人姓名", verbose_name="联系人姓名")
    contact_phone = models.CharField(max_length=32, help_text="联系电话", verbose_name="联系电话")
    address = models.TextField(help_text="地址信息", verbose_name="地址信息")
    email = models.EmailField(help_text="电子邮件", verbose_name="电子邮件")
    website = models.URLField(help_text="官方网站", verbose_name="官方网站")

    creater = models.CharField(max_length=32, null=False, blank=False, help_text="创建人", verbose_name="创建人")
    created_at = models.DateTimeField(auto_now_add=True, help_text="创建时间", verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, help_text="更新时间", verbose_name="更新时间")

    def __str__(self):
        # 返回展示的数据
        return self.publisher_name

    class Meta:
        # 指定表名
        db_table = "publisher"


# 定义书架模型
class BookshelfModel(models.Model):
    # 定义选择项
    SHELF_LOCATION = (
        (1, "逸夫馆"),
        (2, "经济管理学院图书情报中心 "),
        (3, "建筑学院图书馆"),
        (4, "法学院图书馆"),
        (5, "人文社科图书馆"),
        (6, "美术学院图书馆"),
        (7, "医学院图书馆")
    )

    shelf_name = models.CharField(max_length=32, null=False, blank=False, help_text="书架的名称", verbose_name="书架的名称")
    shelf_location = models.IntegerField(choices=SHELF_LOCATION, help_text="书架位置", verbose_name="书架位置", default=1)
    capacity = models.IntegerField(help_text="容量", verbose_name="容量")

    creater = models.CharField(max_length=32, null=False, blank=False, help_text="创建人", verbose_name="创建人")
    created_at = models.DateTimeField(auto_now_add=True, help_text="创建时间", verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, help_text="更新时间", verbose_name="更新时间")

    def __str__(self):
        # 返回展示的数据
        return f"{self.get_shelf_location_display()} / {self.shelf_name}"

    class Meta:
        # 指定表名
        db_table = "bookshelf"


# 定义图书模型
class BooksModel(models.Model):
    book_id = models.AutoField(primary_key=True, null=False, blank=False, help_text="图书id", verbose_name="图书id")
    isbn = models.CharField(max_length=64, null=False, blank=False, help_text="图书编号", verbose_name="图书编号")
    book_name = models.CharField(max_length=64, null=False, blank=False, help_text="图书名称", verbose_name="图书名称")
    author = models.CharField(max_length=32, null=False, blank=False, help_text="作者", verbose_name="作者")
    publish_date = models.DateField(help_text="出版日期", verbose_name="出版日期")

    publisher = models.ForeignKey(PublisherModel, on_delete=models.CASCADE, db_constraint=False,
                                  related_name='books', help_text="出版社",
                                  verbose_name="出版社")
    bookshelf = models.ForeignKey(BookshelfModel, on_delete=models.CASCADE, db_constraint=False,
                                  related_name='books', help_text="归属书架",
                                  verbose_name="归属书架")

    creater = models.CharField(max_length=32, null=False, blank=False, help_text="创建人", verbose_name="创建人")
    created_at = models.DateTimeField(auto_now_add=True, help_text="创建时间", verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, help_text="更新时间", verbose_name="更新时间")

    def __str__(self):
        # 返回展示的数据
        return self.book_name

    class Meta:
        # 指定表名
        db_table = "books"
